import { Injectable, Logger } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { shaderList } from './shader.schema';
import { createShader, updateShader } from './shader.interface';

@Injectable()
export class ShaderService {
    private readonly logger = new Logger(ShaderService.name);
    constructor(
        @InjectModel('shader')
        private readonly Shader: Model<shaderList>,
    ) { }
    async queryShaderList(params: {
        page?: number;
        pageSize?: number;
        name?: string;
    }) {
        try {
            const condition: { [key: string]: string | number | RegExp } = {};
            if (params.name) {
                condition.logisticsName = new RegExp(params.name.trim());
            }
            const list = await this.Shader.find(condition)
                .skip(params.pageSize * (params.page - 1))
                .limit(params.pageSize);
            const total = await this.Shader.countDocuments(condition);
            return { list, total };
        } catch (err) {
            this.logger.error(err);
            return null;
        }
    }

    async queryOneShader(id: string) {
        try {
            return await this.Shader.findOne({ _id: id });
        } catch (err) {
            this.logger.error(err);
            return null;
        }
    }

    async createOneShader(data: createShader) {
        try {
            const repetition = await this.Shader.findOne({
                title: data.title,
            });
            if (repetition) {
                return 'repetition';
            }
            const createRet = await this.Shader.create(data);
            return createRet;
        } catch (err) {
            this.logger.error(err);
            return null;
        }
    }

    async updateShader(data: updateShader): Promise<any> {
        try {
            const repetition = await this.Shader.findOne({
                title: data.title,
            });
            if (repetition) {
                return 'repetition';
            }
            const updateRet = await this.Shader.updateOne(
                { _id: data._id },
                {
                    $set: data,
                },
            );
            return updateRet;
        } catch (err) {
            this.logger.error(err);
            return null;
        }
    }

    async deleteShader(id: string): Promise<any> {
        try {
            const res = await this.Shader.deleteOne({ _id: id });
            return res;
        } catch (err) {
            this.logger.error(err);
            return null;
        }
    }

    // async logisticsOptions(params: getShaderOption) {
    //     const { logisticsOption, page_size, _id } = params;
    //     try {
    //         const condition: { [key: string]: string | number | RegExp } = {};
    //         if (logisticsOption) {
    //             condition.logisticsName = new RegExp(logisticsOption.trim());
    //         }
    //         if (_id) {
    //             condition._id = _id; //精确查找
    //         }
    //         const list = await this.Shader.find(condition).limit(page_size);
    //         const total = await this.Shader.countDocuments(condition);
    //         const options = list.map((item) => ({
    //             id: item._id,
    //             logisticsName: item.logisticsName,
    //             ...item,
    //         }));
    //         return { list: options, total: total };
    //     } catch (err) {
    //         this.logger.error(err);
    //         return null;
    //     }
    // }
}
